package 其他.猿辅导;

public class Main {
    public static void main(String[] args) {
        String str = "[][][[]2]3 ";
        String s = preHandle(str);
        solution(s);
        System.out.println(ans);
    }
    static int idx = 0;
    static int ans = 0;
    static int count = 0;

    static String preHandle(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            sb.append(str.charAt(i));
            if (str.charAt(i) == ']' && (i + 1 == str.length() || str.charAt(i + 1) == '[' || str.charAt(i + 1) == ']')) {
                sb.append(1);
            }
        }
        sb.append('[');
        return sb.toString();
    }

    static void solution(String str) {
        if (idx == str.length()) return;
        while (idx < str.length()) {
            char curr = str.charAt(idx++);
            if (curr == '[') {
                ans += count;
                count = 0;
                solution(str);
            } else if (curr == ']') {
                count++;
            } else {
                count *= Integer.parseInt(curr + "");
                return;
            }
        }
    }
}